﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;
using AutoMapper;


namespace KernelWorkflow.Activities
{
    public partial class GetListOfBookWhichPersonGetInLibrary : SequenceActivity
    {
        public static readonly string GetListOfBooksGotByPersonAccessObject = "GetListOfBookWhichPersonGetInLibrary";

        public static DependencyProperty CurrentSessionProperty = DependencyProperty.Register("CurrentSession", typeof(SessionDto), typeof(GetListOfBookWhichPersonGetInLibrary));
        public static DependencyProperty CurrentPersonGuidProperty = DependencyProperty.Register("CurrentPersonGuid", typeof(Guid), typeof(GetListOfBookWhichPersonGetInLibrary));
        //out
        public static DependencyProperty BookListWithPersonProperty = DependencyProperty.Register("BookListWithPerson", typeof(BooksWithPersonListDto), typeof(GetListOfBookWhichPersonGetInLibrary));

        [Category("AUTHORIZATION_INPUT")]
        [Browsable(true)]
        public SessionDto CurrentSession
        {
            get
            {
                return ((SessionDto)(base.GetValue(GetListOfBookWhichPersonGetInLibrary.CurrentSessionProperty)));
            }
            set
            {
                base.SetValue(GetListOfBookWhichPersonGetInLibrary.CurrentSessionProperty, value);
            }
        }

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public Guid CurrentPersonGuid
        {
            get
            {
                return ((Guid)(base.GetValue(GetListOfBookWhichPersonGetInLibrary.CurrentPersonGuidProperty)));
            }
            set
            {
                base.SetValue(GetListOfBookWhichPersonGetInLibrary.CurrentPersonGuidProperty, value);
            }
        }

        [Category("DATA_OUTPUT")]
        [Browsable(true)]
        public BooksWithPersonListDto BookListWithPerson
        {
            get
            {
                return ((BooksWithPersonListDto)(base.GetValue(GetListOfBookWhichPersonGetInLibrary.BookListWithPersonProperty)));
            }
            set
            {
                base.SetValue(GetListOfBookWhichPersonGetInLibrary.BookListWithPersonProperty, value);
            }
        }

        private bool crutch_first_time = true;
        private Boolean isSessionOk = false;

        private void BookListWithPerson_ExecuteCode(object sender, EventArgs e)
        {
            if (crutch_first_time)
            {
                Mapper.CreateMap<TeacherDto, PersonDto>();
                Mapper.CreateMap<StudentDto, PersonDto>();
                crutch_first_time = false;
            }

            AuthorizationService.AuthorizationClient authclient = new AuthorizationService.AuthorizationClient();
            isSessionOk = authclient.CheckRights(CurrentSession, GetListOfBooksGotByPersonAccessObject);
            authclient.Close();
            Console.WriteLine("GetListOfBooksWhitchPersonGotInLibrary::BookListWithPerson_ExecuteCode isSessionOk={0}", isSessionOk);

            if (isSessionOk == true)
            {

                LibraryService.LibraryServiceClient libraryclient = new LibraryService.LibraryServiceClient();
                BooksListDto booklist = libraryclient.GetBooksGotByPerson(CurrentSession, CurrentPersonGuid);
                libraryclient.Close();

                BookListWithPerson = new BooksWithPersonListDto();
                BookListWithPerson.Books = booklist.Books;

                HrService.HrServiceClient hrclient = new HrService.HrServiceClient();

                PersonDto person = hrclient.GetStudent(CurrentSession, booklist.HolderPersonGuid);
                if (person.Id.Equals(Guid.Empty))
                {
                    person = hrclient.GetTeacher(CurrentSession, booklist.HolderPersonGuid);
                    BookListWithPerson.HolderPerson = Mapper.Map<TeacherDto, PersonDto>(person as TeacherDto);
                }
                else
                    BookListWithPerson.HolderPerson = Mapper.Map<StudentDto, PersonDto>(person as StudentDto);

                hrclient.Close();
            }
        }

        public GetListOfBookWhichPersonGetInLibrary()
        {
            InitializeComponent();
        }
    }
}
